/**
 * Created by novem on 2015/10/26.
 */
function pi(){
    return  3.1415926535898;
}
pow=Math.pow;
sin=Math.sin;
cos=Math.cos;
asin=Math.asin;
min=Math.min;
sqrt=Math.sqrt;
window.onload=function(){
    var cameraId = $.getUrlParam("cameraId");
    var userid=getUserId();
    var url=getUrl();
    var loc=getLoc();
    $("#aaaa").attr('href','xiedianping.html?id='+cameraId+'&param=sheying');
    $("#jubao").attr('href','jubao.html?objId='+cameraId+"&typy=8")
    $.jsonp({
        url: 'http://'+url+'/api/mapline/camera/detail-'+cameraId,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            earthRadius = 6371;
            var lat0 = (loc.lat * pi()) / 180;
            var lng0 = (loc.lng * pi()) / 180;
            var lng1 = (data.cameraDimensions * pi()) / 180;
            var lat1 = (data.cameraLongitude * pi()) / 180;
            calcLongitude = lng1 - lng0;
            calcLatitude = lat1 - lat0;
            stepOne = pow(sin(calcLatitude / 2), 2) + cos(lat0) * cos(lat1) * pow(sin(calcLongitude / 2), 2);
            stepTwo = 2 * asin(min(1, sqrt(stepOne)));
            calculatedDistance = earthRadius * stepTwo;
            distance=calculatedDistance.toFixed(1);
            data.distance=distance;
            $("#telenum").attr("href","tel:"+data.telphone);
            $(".bacim").attr('style','background-image:url('+data.img+')');
            var complied_tpl = juicer($("#sheying_tpl").text());
            var html = complied_tpl.render(data);
            $(".info_detail").append(html);
            $.jsonp({
                url: 'http://'+url+'/api/mapline/comment_list-'+cameraId,
                data: {},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $("#all_pj").attr('href','sheying_allpingjia.html?cameraId=' + cameraId);
                    var len = data.arrays.length-1;
                    if(len<0){
                        var html = "";
                    }else {
                        var unixTimestamp = new Date(data.arrays[0].createTime * 1000);
                        data.arrays[0].createTime = unixTimestamp.toLocaleString();
                        var html = '<li class="ui-border-b"><div class="ui-comment-list-detail clearfix"><div class="ui-comment-list-user clearfix"><div class="ui-comment-list-avatar">' +
                            '<img src="' + data.arrays[0].photo + '" width="40" height="40" alt=""></div>' +
                            '<div class="ui-comment-list-info"><p>' + data.arrays[0].nickName + '</p><p class="ui-txt-muted">' + data.arrays[0].createTime + '</p></div></div>' +
                            '<div class="ui-comment-list-star"><span class="ui-star ui-star-' + data.arrays[0].star + '"></span>' + data.arrays[0].star + '分</div></div>' +
                            '<div class="ui-comment-list-content">' +
                            '' + data.arrays[0].comment + '</div></li>' ;
                    }
                    $("#ui-comment-list").append(html);
                },
                error: function (xOptions, textStatus) {}
            });
            $.jsonp({
                url: 'http://'+url+'/api/collect/collect_camera_list',
                data: {userId:userid},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays,function(index,ele){
                        var samewayId = ele.cameraId;
                        if(samewayId==cameraId){
                            $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                        }
                    })
                },
                error: function (xOptions, textStatus) {}
            });
        },
        error: function (xOptions, textStatus) {
        }
    });
    //收藏
    $(document).on('click','.w_shac',function(){
        $.jsonp({
            url: 'http://'+url+'/api/collect/collect_camera',
            data: {cameraId:cameraId,userId:userid},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                var code = data.code;
                if(code==1) {
                    $(".sc").removeClass("btn").text("收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("取消收藏成功")
                }else{
                    $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("收藏成功")
                }
            },
            error: function (xOptions, textStatus) {
            }
        });

    });
}